package com.IntelligentCommunity.community.service.impl;

import com.IntelligentCommunity.community.domain.RealBuilding;
import com.IntelligentCommunity.community.domain.RealElement;
import com.IntelligentCommunity.community.domain.RealEstate;
import com.IntelligentCommunity.community.mapper.RealEstateMapper;
import com.IntelligentCommunity.community.request.ElementRequest;
import com.IntelligentCommunity.community.request.EstateBathInsertRequest;
import com.IntelligentCommunity.community.service.IRealBuildingService;
import com.IntelligentCommunity.community.service.IRealElementService;
import com.IntelligentCommunity.community.service.IRealEstateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 实有房屋Service业务层处理
 *
 * @author IntelligentCommunity
 * @date 2024-07-21
 */
@Service
public class RealEstateServiceImpl extends ServiceImpl<RealEstateMapper, RealEstate> implements IRealEstateService
{
    @Autowired
    private RealEstateMapper realEstateMapper;

    @Autowired
    private IRealElementService elementService;

    @Autowired
    private IRealBuildingService buildingService;

    /**
     * 查询实有房屋
     *
     * @param id 实有房屋主键
     * @return 实有房屋
     */
    @Override
    public RealEstate selectRealEstateById(Long id)
    {
        return realEstateMapper.selectRealEstateById(id);
    }

    /**
     * 查询实有房屋列表
     *
     * @param realEstate 实有房屋
     * @return 实有房屋
     */
    @Override
    public List<RealEstate> selectRealEstateList(RealEstate realEstate)
    {
        return realEstateMapper.selectRealEstateList(realEstate);
    }

    /**
     * 新增实有房屋
     *
     * @param realEstate 实有房屋
     * @return 结果
     */
    @Override
    public int insertRealEstate(RealEstate realEstate)
    {
        return realEstateMapper.insertRealEstate(realEstate);
    }

    /**
     * 修改实有房屋
     *
     * @param realEstate 实有房屋
     * @return 结果
     */
    @Override
    public int updateRealEstate(RealEstate realEstate)
    {
        return realEstateMapper.updateRealEstate(realEstate);
    }

    /**
     * 批量删除实有房屋
     *
     * @param ids 需要删除的实有房屋主键
     * @return 结果
     */
    @Override
    public int deleteRealEstateByIds(Long[] ids)
    {
        return realEstateMapper.deleteRealEstateByIds(ids);
    }

    /**
     * 删除实有房屋信息
     *
     * @param id 实有房屋主键
     * @return 结果
     */
    @Override
    public int deleteRealEstateById(Long id)
    {
        return realEstateMapper.deleteRealEstateById(id);
    }

    @Transactional
    @Override
    public void bathInsert(EstateBathInsertRequest estateBathInsertRequest) {

        List<RealEstate> realEstateList = new ArrayList<>();
        List<RealElement> realElementList = new ArrayList<>();

        //获取小区id
        Long districtId = estateBathInsertRequest.getDistrictId();
        String buildingName = estateBathInsertRequest.getBuildingName();

        RealBuilding realBuilding = new RealBuilding();
        // TODO 添加楼栋信息
        realBuilding.setName(String.valueOf(buildingName));
        realBuilding.setDistrictId(districtId);
        buildingService.save(realBuilding);

        Long buildingId = realBuilding.getId();

        List<ElementRequest> elementList = estateBathInsertRequest.getElementList();

        for (ElementRequest elementRequest : elementList) {
            // 开始楼层
            Long startFloor = elementRequest.getStartFloor();
            // 结束楼层
            Long endFloor = elementRequest.getEndFloor();
            // 开始房间号
            Long startEstate = elementRequest.getStartEstate();
            // 房间数量
            Long estateNum = elementRequest.getEstateNum();
            // 单元数量
            Long elementId = elementRequest.getElementId();

            // 添加单元信息
            RealElement realElement = new RealElement();
            realElement.setBuildingId(buildingId);
            realElement.setName(String.valueOf(elementRequest.getElementId()));
            realElementList.add(realElement);


            for (Long i = startFloor; i <= endFloor; i++) {
                for (Long j = startEstate; j <= estateNum; j++) {
                    RealEstate realEstate = new RealEstate();
                    realEstate.setDistrictId(districtId);
                    realEstate.setRidgepole(buildingId);
                    realEstate.setElement(elementId);
                    realEstate.setTier(i);
                    realEstate.setHouseholds(i * 100 + j);
                    realEstateList.add(realEstate);
                }
            }

        }

        elementService.saveBatch(realElementList);
        saveBatch(realEstateList);


    }

    @Override
    public List<RealEstate> selectRealEstateListByPopulationId(Integer populationId) {
        return realEstateMapper.selectRealEstateListByPopulationId(populationId);
    }

}
